using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using BlueGameComponent;

namespace AirForce
{
    class MainMenuScreen : MenuScreen
    {
        static String SoundOnText = "Sound:On";
        static String SoundOffText = "Sound:Off";

        Texture2D background;
        TouchMenuEntry switchSoundEntry;
        public MainMenuScreen()
            : base("")
        {
            // Create our menu entries.
            TouchMenuEntry startGameMenuEntry = new TouchMenuEntry("Start");
            TouchMenuEntry highScoreMenuEntry = new TouchMenuEntry("High Score");
            switchSoundEntry = new TouchMenuEntry(GetDisplayEntryText());
            TouchMenuEntry creditsMenuEntry = new TouchMenuEntry("Credits");
            TouchMenuEntry exitMenuEntry = new TouchMenuEntry("Exit");
            

            // Hook up menu event handlers.
            startGameMenuEntry.Selected += StartSelected;
            highScoreMenuEntry.Selected += HighScoreSelected;
            switchSoundEntry.Selected += SwitchSoundSelected;
            creditsMenuEntry.Selected += CreditsSelected;
            exitMenuEntry.Selected += ExitSelect;

            // Add entries to the menu.
            MenuEntries.Add(startGameMenuEntry);
            MenuEntries.Add(highScoreMenuEntry);
            MenuEntries.Add(switchSoundEntry);
            MenuEntries.Add(creditsMenuEntry); 
            MenuEntries.Add(exitMenuEntry);

            AudioManager.StopMusic();
            AudioManager.PlayMusic("Sounds/menuMusic");
        }

        public override void  LoadContent()
        {
 	        base.LoadContent();
            background = ScreenManager.Game.Content.Load<Texture2D>("Textures/mainMenu");
        }
       /* protected override void UpdateMenuEntryLocations()
        {
            int menuEntryHorizontalPlacement;
            int menuEntryVerticalPlacement = GameConstants.MainMenuTop;

            // update each menu entry's location in turn
            for (int i = 0; i < MenuEntries.Count; i++)
            {
                float entryWidth = Font.MeasureString(MenuEntries[i].Text).X;
                menuEntryHorizontalPlacement = (int)(GameConstants.MainMenuLeft + maxEntryWidth / 2 - entryWidth / 2);
                MenuEntries[i].Position = new Vector2(menuEntryHorizontalPlacement, menuEntryVerticalPlacement);
                menuEntryVerticalPlacement += GameConstants.MainMenuEntryGap;
            }
          
        }
*/

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            spriteBatch.Begin();
            spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
            //DrawTransition(background, new Vector2(0, 0), Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
        private void StartSelected(object sender, EventArgs e)
        {
            AudioManager.PlaySound("Sounds/Menu_Selection");
            ScreenManager.RemoveAllScreen();
        }
        private void HighScoreSelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new HighScoreScreen(),null);
        }
        protected void ExitSelect(object sender, EventArgs e)
        {
            ScreenManager.RemoveAllScreen();
            return;
        }
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            ScreenManager.Game.Exit();
            return;
        }
        public override void OnExit()
        {
            if ("Start" == lastSelectedEnteryText)
            {
                ScreenManager.AddScreen(new GameScreen(), null);
            }
            else if ("Exit" == lastSelectedEnteryText)
            {
                ScreenManager.Game.Exit();
            }
        }
        private void SwitchSoundSelected(object sender, EventArgs e)
        {
            AudioManager.Switch = !AudioManager.Switch;
            if (!AudioManager.Switch)
            {
                AudioManager.StopMusic();
            }
            else
            {
                AudioManager.PlayMusic("Sounds/menuMusic");
            }
            AudioManager.PlaySound("Sounds/Menu_Selection");
            switchSoundEntry.Text = GetDisplayEntryText();
        }
        private void CreditsSelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new CreditsScreen(), null);
        }
        
        private String GetDisplayEntryText()
        {
            if (AudioManager.Switch)
            {
                return SoundOnText;
            }
            else
            {
                return SoundOffText;
            }
        }
        
    }


}
